/*
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 */

//
//  AppDelegate.m
//  MyApp
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "VideoPrizeClawViewController.h"

#import "AppDelegate+MMGConfig.h"
#import "AppDelegate+MMGBackgroundTask.h"
#import "MMGPrizeClawGameControlHandler.hpp"
#import "AppDelegate+MMGJPush.m"

#ifdef DEBUG
#import <FBMemoryProfiler/FBMemoryProfiler.h>
#import <FBRetainCycleDetector/FBRetainCycleDetector.h>
#import "CacheCleanerPlugin.h"
#import "RetainCycleLoggerPlugin.h"

#endif

@implementation AppDelegate {
#ifdef DEBUG
    FBMemoryProfiler *memoryProfiler;
#endif
}

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    //配置toast
    [self mmg_configSVProgressHUD];
    // 设置第三方库配置：微信、等
    [self mmg_venderConfig];
 
    // 配置保持后台运行
    [self mmg_backgroundTask];
    
    //极光推送
    [self configJPushPlatformsWithOptions:launchOptions];
    //第一次进入需要初始化控制socket
    MMGPrizeClawGameControlHandler::getInstance()->initGameproto();
    //调试
#ifdef DEBUG
    NSArray *filters = @[FBFilterBlockWithObjectIvarRelation([UIView class], @"_subviewCache"),
                         FBFilterBlockWithObjectIvarRelation([UIPanGestureRecognizer class], @"_internalActiveTouches")];
    
    FBObjectGraphConfiguration *configuration =
    [[FBObjectGraphConfiguration alloc] initWithFilterBlocks:filters
                                         shouldInspectTimers:NO];
    
    memoryProfiler = [[FBMemoryProfiler alloc] initWithPlugins:@[[CacheCleanerPlugin new],
                                                                 [RetainCycleLoggerPlugin new]]
                              retainCycleDetectorConfiguration:configuration];
    [memoryProfiler enable];
#endif
     
    
    DLog(@"APPLICATION SELF VIEW CONTROLLER");
    DLog(@" [[UIScreen mainScreen] currentMode].size %@",NSStringFromCGSize( [[UIScreen mainScreen] currentMode].size));
//    UINavigationController *videoNavVC = [[UINavigationController alloc] initWithRootViewController:[[VideoPrizeClawViewController alloc] init]];
//    self.viewController = (CDVViewController *)videoNavVC;
    //UINavigationController *root = [[UINavigationController alloc] initWithRootViewController: [[MainViewController alloc] init]];

    self.viewController = [[MainViewController alloc] init];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [self mmg_handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [self mmg_handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
    return [self mmg_handleOpenURL:url];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    //必须保证在发送连接命令前初始化
    [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];
    MMGPrizeClawGameControlHandler::getInstance()->initGameproto();
}
- (void)applicationWillTerminate:(UIApplication *)application {
    MMGPrizeClawGameControlHandler::getInstance()->uninitGameproto();
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
//    [self mmg_backgroundTask];
    MMGPrizeClawGameControlHandler::getInstance()->uninitGameproto();
    
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    /// Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
    
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // Required, iOS 7 Support
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required,For systems with less than or equal to iOS6
    [JPUSHService handleRemoteNotification:userInfo];
}





@end
